package com.group5.service;


import com.group5.pojo.Platform;
import com.group5.pojo.PlatformExample;
import com.group5.pojo.Userss;
import com.group5.pojo.UserssExample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RegisterService {

    //1代表临时机构 2卖方系统管理员 3 买方系统管理员 4 厂商系统管理员 5 运营商系统管理员 6 药品全档管理员; 56是一类,234是一类机构管理员 7 卖方业务员 8 卖方开票员 9买方业务员
    protected static HashMap<String,Integer> AuthorityCode= new HashMap<String,Integer>(){
        {
            put("卖方业务员",7);
            put("买方业务员",9);
        }
    };

    public UserssExample selectUserByUserNameExample(String username){
        UserssExample userssExample=new UserssExample();
        userssExample.or(userssExample.createCriteria().andUser_nameEqualTo(username));
        return userssExample;
    }

    public PlatformExample selectPlatformByONameExample(String oname){
        PlatformExample platformExample=new PlatformExample();
        platformExample.or(platformExample.createCriteria().andOnameEqualTo(oname));
        return platformExample;
    }

    public PlatformExample selectPlatformByOmnemonics_codeExample(String omnemonics_code){
        PlatformExample platformExample=new PlatformExample();
        platformExample.or(platformExample.createCriteria().andOmnemonics_codeEqualTo(omnemonics_code));
        return platformExample;
    }

    public Userss MD5UserPasswordAndSalt(Userss userss){
        String salt= Md5Util.saltGenerate();
        userss.setSalt(salt);
        userss.setPassword(Md5Util.Md5Generate(userss.getPassword(),salt));
        return userss;
    }

//    {
//        "oname": "测试厂商4",
//            "ommemonics_code": 1234566,
//            "seller_state": 2
//    }
    public void initPlatform(Platform platform){
        platform.setOrole_type(0);
        platform.setOstate(true);
        platform.setSeller_state(platform.getSeller_state()==null?0: platform.getSeller_state());
        platform.setBuyer_state(platform.getBuyer_state()==null?0: platform.getBuyer_state());
        platform.setManufacturer_state(platform.getManufacturer_state()==null?0:platform.getManufacturer_state());
        platform.setPlatform_operator(0);
    }



    public void initUser(Userss userss){
        userss.setUser_state(true);
        userss.setUser_authority(1);
        userss.setIs_organization_accept(0);
    }

    public boolean isDraft(Platform platform){
        if(platform.getSeller_state()==1||platform.getSeller_state()==3||platform.getBuyer_state()==1||
                platform.getBuyer_state()==3||platform.getManufacturer_state()==1||platform.getManufacturer_state()==3)
            return true;
        else return false;
    }

    public void setDraftToUnaudited(Platform platform){
        if(platform.getSeller_state()==1||platform.getSeller_state()==3){
            platform.setSeller_state(2);
        }
        else if(platform.getBuyer_state()==1||platform.getBuyer_state()==3){
            platform.setBuyer_state(2);
        }
        else if(platform.getManufacturer_state()==1||platform.getManufacturer_state()==3)
            platform.setManufacturer_state(2);
    }

    public int getAuthAuthorityCodeByString(String authority){
        return AuthorityCode.get(authority);
    }
//0默认.啥也不是. 8逻辑删除 1卖方、2买方、3厂商、4卖方厂商、5卖方买方、6买方厂商、7卖方买方厂商， 0 啥也不是
    public List<AuthorityString> getAllAuthorityByPlatform(Platform platform) {
        List<AuthorityString> authority=new ArrayList<>();
        switch (platform.getOrole_type()){
            case 1:
            case 4:
            {
                authority.add(new AuthorityString("卖方业务员"));

                break;
            }
            case 2:
            case 6:
            {
                authority.add(new AuthorityString("买方业务员"));
                break;
            }
            case 5:
            case 7:
            {
                authority.add(new AuthorityString("卖方业务员"));

                authority.add(new AuthorityString("买方业务员"));
                break;
            }
            default:
                break;
        }
        return authority;
    }
    public static class AuthorityString {
        String authority;
        AuthorityString(String authority){
            this.authority=authority;
        }
        AuthorityString(){

        }

        public String getAuthority() {
            return authority;
        }
    }
}

